package test;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

import lattice.Computation;
import lattice.ConsistentStates;
import lattice.Evaluable;
import lattice.traveler.State;
import lattice.traveler.VectorClock;

public class PartialOrderReader implements Evaluable {
	public boolean evaluate(ConsistentStates cs) {
		int x = (Integer) cs.getState(0);
		int y = (Integer) cs.getState(1);
		
		if ((x == 0) && (y == 1)) {
			System.out.println("CGS" + cs + " State[" + x + "," + y + "]");
		}
		return false;
	}
	
	
	public static Computation[] parse() {
		Computation[] computations = new Computation[3];
		FileInputStream fstream = null;
		DataInputStream in = null;
		BufferedReader reader = null;
		try {
			fstream = new FileInputStream("PartialOrder.txt");
			in = new DataInputStream(fstream);
			reader = new BufferedReader(new InputStreamReader(in));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		for (int k = 0; k < 3; ++k) {
			computations[k] = new Computation(2);
			try {
				reader.readLine(); // begin a round
				for (int i = 0; i < 6; i++) {
					StringTokenizer st = new StringTokenizer(reader.readLine());
					int ID = Integer.parseInt(st.nextToken()) - 1;
					int value = Integer.parseInt(st.nextToken());
					int TS1 = Integer.parseInt(st.nextToken());
					int TS2 = Integer.parseInt(st.nextToken());
					
					State e = new State();
					e.clock = new VectorClock(2, "1/"+TS1+",2/"+TS2);
					e.data = value;
					computations[k].addState(ID, e);
				}
				reader.readLine(); // end a round
			} catch (IOException e) {e.printStackTrace();}
		}
		
		return computations;
	}
}
